package br.com.spam.pipeline.instruction;

import br.com.spam.pipeline.core.Configuration;
import br.com.spam.pipeline.core.Register;

public class RsRtLoader extends RsLoader
{
	private final Register rt;
	private Integer loadedValue;

	public RsRtLoader(Register rs, Register rt, Instruction instruction)
	{
		super(rs, instruction);
		this.rt = rt;
	}

	// Tá bronco, mas caguei
	@Override
	public boolean instructionDecodeHook()
	{
		switch (this.rt.getCorrectValueDataSource())
		{
			case JustCalculated :
				if (Configuration.bypassEnabled)
				{
					this.instruction.setALUSrcB(ALUSrc.EX);
					return super.instructionDecodeHook();
				}
				else
					return false;
			case JustLoadedFromMemory :
				if (Configuration.bypassEnabled)
				{
					this.instruction.setALUSrcB(ALUSrc.DM_Memory);
					return super.instructionDecodeHook();
				}
				else
					return false;
			case EX :
				if (Configuration.bypassEnabled)
				{
					this.instruction.setALUSrcB(ALUSrc.DM_EX);
					return super.instructionDecodeHook();
				}
				else
					return false;

			case DM_EX :
			case DM_Memoria :
			case Register :
				this.instruction.setALUSrcB(ALUSrc.Register);
				this.loadedValue = this.rt.getCorrectValue();
				return super.instructionDecodeHook();
			default :
				return false;
		}
	}

	@Override
	public void executionHook()
	{
		super.executionHook();

		if (this.loadedValue == null)
		{
			this.loadedValue = this.rt.getCorrectValue();
		}
	}

	public int rtValue()
	{
		return this.loadedValue;
	}
}
